package com.getpebble.android.framework.endpoint;

import android.content.Context;
import android.text.TextUtils;
import com.getpebble.android.bluetooth.protocol.ProtocolMessage;
import com.getpebble.android.common.core.trace.Trace;
import com.getpebble.android.common.model.FrameworkState;
import com.getpebble.android.framework.appmessage.AppLifecycleMessage;
import com.getpebble.android.framework.appmessage.AppLifecycleMessage.RunState;
import com.getpebble.android.framework.appmessage.AppMessage;
import com.getpebble.android.framework.appmessage.AppMessage.AppMessageCommand;
import com.getpebble.android.framework.endpoint.EndpointRequest.EndpointAction;
import com.getpebble.android.framework.endpoint.EndpointRequest.EndpointArgumentKeys;
import com.getpebble.android.framework.pebblekit.PebbleKit;
import com.getpebble.android.framework.protocol.EndpointId;
import com.getpebble.android.framework.protocol.inbound.PblInboundAppLifecycleMessage;
import com.getpebble.android.framework.protocol.inbound.PblInboundAppMessage;
import com.getpebble.android.framework.protocol.outbound.PblOutboundAppLifecycleMessage;
import com.getpebble.android.framework.protocol.outbound.PblOutboundAppMessage;
import com.getpebble.android.jskit.bridge.JsAppLifecycleListener;
import com.google.common.collect.ImmutableSet;
import com.sun.mail.imap.protocol.ListInfo;
import com.sun.mail.smtp.SMTPMessage;
import java.util.Set;
import java.util.UUID;

public class AppMessageEndpoint extends RequestableEndpoint {
    private final Context mContext;
    private final IEndpointMessageSender mMessageSender;
    private final PebbleKit mPebbleKit;

    static /* synthetic */ class AnonymousClass1 {
        static final /* synthetic */ int[] $SwitchMap$com$getpebble$android$framework$appmessage$AppMessage$AppMessageCommand = new int[AppMessageCommand.values().length];

        static {
            try {
                $SwitchMap$com$getpebble$android$framework$appmessage$AppMessage$AppMessageCommand[AppMessageCommand.PUSH.ordinal()] = 1;
            } catch (NoSuchFieldError e) {
            }
            try {
                $SwitchMap$com$getpebble$android$framework$appmessage$AppMessage$AppMessageCommand[AppMessageCommand.ACK.ordinal()] = 2;
            } catch (NoSuchFieldError e2) {
            }
            try {
                $SwitchMap$com$getpebble$android$framework$appmessage$AppMessage$AppMessageCommand[AppMessageCommand.NACK.ordinal()] = 3;
            } catch (NoSuchFieldError e3) {
            }
        }
    }

    public AppMessageEndpoint(Context context, IEndpointMessageSender messageSender, PebbleKit pebbleKit) {
        if (context == null) {
            throw new IllegalArgumentException("'context' cannot be null!");
        } else if (messageSender == null) {
            throw new IllegalArgumentException("'messageSender' cannot be null!");
        } else if (pebbleKit == null) {
            throw new IllegalArgumentException("'pebbleKit' cannot be null!");
        } else {
            this.mContext = context;
            this.mMessageSender = messageSender;
            this.mPebbleKit = pebbleKit;
        }
    }

    Set<EndpointId> getSupportedEndpoints() {
        return ImmutableSet.of(EndpointId.APP_LIFECYCLE, EndpointId.APP_MESSAGE);
    }

    boolean onRequest(EndpointRequest request, FrameworkState frameworkState) {
        Trace.verbose("AppMessageEndpoint", "onRequest");
        if (!isRequestSupported(request)) {
            Trace.debug("AppMessageEndpoint", "Request is not supported.");
            return false;
        } else if (request.getEndpointId() == EndpointId.APP_MESSAGE) {
            return handleAppMessageRequest(request);
        } else {
            if (request.getEndpointId() == EndpointId.APP_LIFECYCLE) {
                return handleAppLifecycleRequest(request);
            }
            return true;
        }
    }

    protected boolean handleAppLifecycleRequest(EndpointRequest request) {
        EndpointAction action = request.getAction();
        PblOutboundAppLifecycleMessage outboundAppLifecycleMessage = null;
        if (action == EndpointAction.REQUEST_RUNNING_APP) {
            Trace.verbose("AppMessageEndpoint", "action: REQUEST_RUNNING_APP");
            outboundAppLifecycleMessage = PblOutboundAppLifecycleMessage.createFetchMessage();
        } else if (action == EndpointAction.START_APP || action == EndpointAction.STOP_APP) {
            Trace.verbose("AppMessageEndpoint", "action: " + action);
            String uuidString = request.getString(EndpointArgumentKeys.UUID);
            if (TextUtils.isEmpty(uuidString)) {
                Trace.error("AppMessageEndpoint", "missing UUID in lifecycle message");
                return false;
            }
            UUID uuid = UUID.fromString(uuidString);
            if (uuid == null) {
                Trace.error("AppMessageEndpoint", "invalid UUID in lifecycle message");
                return false;
            }
            outboundAppLifecycleMessage = action == EndpointAction.START_APP ? PblOutboundAppLifecycleMessage.createStartMessage(uuid) : PblOutboundAppLifecycleMessage.createStopMessage(uuid);
        }
        if (outboundAppLifecycleMessage == null || !this.mMessageSender.sendMessage(outboundAppLifecycleMessage)) {
            Trace.debug("AppMessageEndpoint", "send failed");
            return false;
        }
        Trace.verbose("AppMessageEndpoint", "outbound>> lifecycle command = " + action + " transactionId = " + outboundAppLifecycleMessage.getAppMessage().getTransactionId());
        return true;
    }

    protected boolean handleAppMessageRequest(EndpointRequest request) {
        AppMessage appMessage = (AppMessage) request.getParcelable(EndpointArgumentKeys.APP_MESSAGE);
        if (appMessage == null) {
            Trace.error("AppMessageEndpoint", "missing appmessage");
            return false;
        } else if (request.getAction() == EndpointAction.PUSH_APP_MESSAGE) {
            return sendAppMessage(appMessage);
        } else {
            Trace.error("AppMessageEndpoint", "unsupported action: " + request.getAction());
            return false;
        }
    }

    protected boolean sendAppMessage(AppMessage appMessage) {
        PblOutboundAppMessage outboundAppMessage = new PblOutboundAppMessage(appMessage);
        if (outboundAppMessage == null || !this.mMessageSender.sendMessage(outboundAppMessage)) {
            Trace.debug("AppMessageEndpoint", "appMessage send failed");
            return false;
        }
        Trace.verbose("AppMessageEndpoint", "outbound>> appmessage command = " + appMessage.getCommand() + " transactionId = " + appMessage.getTransactionId() + " uuid = " + appMessage.getUuid());
        return true;
    }

    protected boolean sendAppLifecycleMessage(AppMessage appMessage) {
        PblOutboundAppLifecycleMessage outboundAppLifecyceMessage = new PblOutboundAppLifecycleMessage(appMessage);
        if (outboundAppLifecyceMessage == null || !this.mMessageSender.sendMessage(outboundAppLifecyceMessage)) {
            Trace.debug("AppMessageEndpoint", "appLifecycleMessage send failed");
            return false;
        }
        Trace.verbose("AppMessageEndpoint", "outbound>> lifecycle command = " + appMessage.getCommand() + " transactionId = " + appMessage.getTransactionId());
        return true;
    }

    public boolean sendLifecycleAck(int transactionId) {
        return sendAppLifecycleMessage(new AppMessage((byte) (transactionId & 255), null, AppMessageCommand.ACK, null));
    }

    public boolean sendLifecycleNack(int transactionId) {
        return sendAppLifecycleMessage(new AppMessage((byte) (transactionId & 255), null, AppMessageCommand.NACK, null));
    }

    boolean onPrfRequest(EndpointRequest request, FrameworkState frameworkState) {
        return false;
    }

    boolean onReceive(ProtocolMessage message) {
        short endpoint = message.getEndpointId();
        if (endpoint == EndpointId.APP_MESSAGE.getCode()) {
            PblInboundAppMessage inboundAppMessage = new PblInboundAppMessage(message);
            if (inboundAppMessage.getAppMessage() != null) {
                Trace.verbose("AppMessageEndpoint", "inbound<< appmessage command = " + inboundAppMessage.getAppMessage().getCommand() + " transactionId = " + inboundAppMessage.getAppMessage().getTransactionId() + " uuid = " + inboundAppMessage.getAppMessage().getUuid());
            }
            this.mPebbleKit.handleAppMessage(inboundAppMessage);
            return true;
        } else if (endpoint == EndpointId.APP_LIFECYCLE.getCode()) {
            handleAppLifecycleMessage(message);
            return true;
        } else {
            Trace.debug("AppMessageEndpoint", "unhandled endpoint: " + endpoint);
            return false;
        }
    }

    private boolean handleAppLifecycleMessage(ProtocolMessage protocolMessage) {
        PblInboundAppLifecycleMessage inboundAppLifecycleMessage = new PblInboundAppLifecycleMessage(protocolMessage);
        AppMessage appMessage = inboundAppLifecycleMessage.getAppMessage();
        if (appMessage == null) {
            Trace.warning("AppMessageEndpoint", "appMessage is null");
            return false;
        }
        Trace.verbose("AppMessageEndpoint", "inbound<< lifecycle command = " + appMessage.getCommand() + " transactionId = " + appMessage.getTransactionId() + " uuid = " + appMessage.getUuid());
        switch (AnonymousClass1.$SwitchMap$com$getpebble$android$framework$appmessage$AppMessage$AppMessageCommand[appMessage.getCommand().ordinal()]) {
            case SMTPMessage.RETURN_FULL /*1*/:
                AppLifecycleMessage appLifecycleMessage = inboundAppLifecycleMessage.getLifecycleMessage();
                if (appLifecycleMessage == null) {
                    Trace.warning("AppMessageEndpoint", "appLifecycleMessage is null");
                    sendLifecycleNack(appMessage.getTransactionId());
                    return false;
                }
                RunState runState = appLifecycleMessage.getRunState();
                UUID uuid = appLifecycleMessage.getUUID();
                if (uuid == null) {
                    Trace.warning("AppMessageEndpoint", "uuid is null");
                    sendLifecycleNack(appMessage.getTransactionId());
                    return false;
                } else if (runState == RunState.RUNNING) {
                    Trace.verbose("AppMessageEndpoint", "running: " + uuid);
                    JsAppLifecycleListener.onWatchAppStarted(this.mContext, uuid);
                    break;
                } else if (runState == RunState.NOT_RUNNING) {
                    Trace.verbose("AppMessageEndpoint", "stopped: " + uuid);
                    JsAppLifecycleListener.onWatchAppStopped(this.mContext, uuid);
                    break;
                } else {
                    Trace.error("AppMessageEndpoint", "unhandled lifecycle state: " + runState);
                    return false;
                }
            case SMTPMessage.RETURN_HDRS /*2*/:
                Trace.verbose("AppMessageEndpoint", "lifecycle ACK");
                return true;
            case ListInfo.INDETERMINATE /*3*/:
                Trace.info("AppMessageEndpoint", "lifecycle NACK");
                return true;
        }
        sendLifecycleAck(appMessage.getTransactionId());
        return true;
    }
}
